<head th:replace="common :: common_head(~{},~{})" xmlns:th="http://www.thymeleaf.org">
</head>

<div class="row" xmlns:th="http://www.thymeleaf.org">
    <div class="col-md-12">
        <div class="card">
            <form id="allInputsFormValidation" class="form-horizontal"
                  method="post" novalidate="">
                <div class="content">
                    <legend>修改密码</legend>
                    <input type="hidden" name="id" th:value="${user.id }"/>
                    <input type="hidden" name="state" th:value="${user.state }"/>
                    <input type="hidden" name="name" th:value="${user.name }"/>
                    <input type="hidden" name="phone" th:value="${user.phone }"/>
                    <input type="hidden" name="roleid" th:value="${user.roleid }"/>

                    <!--					<fieldset>-->
                    <!--						<div class="form-group">-->
                    <!--							<label class="col-sm-4 control-label">旧密码<star>*</star></label>-->
                    <!--							<div class="col-sm-6">-->
                    <!--								<input class="form-control" value=""-->
                    <!--									 type="password" required="true" isOldPassword="true" />-->
                    <!--							</div>-->
                    <!--						</div>-->
                    <!--					</fieldset>-->
                    <fieldset>
                        <div class="form-group">
                            <label class="col-sm-4 control-label">新密码
                                <star>*</star>
                            </label>
                            <div class="col-sm-6">
                                <input class="form-control" name="password"
                                       id="registerPassword" type="password" required="true"/>
                            </div>
                        </div>
                    </fieldset>
                    <fieldset>
                        <div class="form-group">
                            <label class="col-sm-4 control-label">确认密码
                                <star>*</star>
                            </label>
                            <div class="col-sm-6">
                                <input class="form-control" id="registerPasswordConfirmation"
                                       type="password"
                                       required="true"
                                       equalTo="#registerPassword"/>
                            </div>
                        </div>
                    </fieldset>


                </div>
                <div class="footer text-center">
                    <button type="submit" class="btn btn-info btn-fill">修改</button>
                    <button type="button" id="reset" class="btn btn-warning btn-fill">重置为初始密码</button>
                    <button type="button" id="back" class="btn btn-success btn-fill">返回</button>
                </div>
            </form>

        </div>
        <!--  end card  -->
    </div>
    <!-- end col-md-12 -->
</div>
<!-- end row -->


<script type="text/javascript">


    $().ready(function () {

        $("#reset").click(function () {
            $.ajax({
                data: $("#allInputsFormValidation").serialize(),
                dataType: "text",
                type: "post",
                url: "/user/resetPassword",
                success: function (data) {
                    console.log(data);
                    if (data == "1") {
                        swal("成功！", "修改成功，2s后退出登录。请重新登录", "success");
                        setTimeout(function () {
                            window.parent.location.href = "/user/loginout";
                        }, 2000)
                    } else {
                        swal("糟糕！", data, "error");
                    }
                },
            });
        });

        $("#back").click(function () {
            window.location.href = "javascript:history.go(-1)"
        })

        //重写验证规则，设置返回中文
        $.validator.messages.required = jQuery.validator.format("请填写必填字段");
        $.validator.messages.equalTo = jQuery.validator.format("两次密码输入不一致");
        // 手机号码验证
        jQuery.validator.addMethod("isMobile", function (value, element) {
            var length = value.length;
            return (length == 11 && /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/.test(value));
        }, "请正确填写您的手机号码。");


        // jQuery.validator.addMethod("isOldPassword", function(value, element) {
        //     return '${user.password }' == value;
        // }, "旧密码错误。");


        $('#allInputsFormValidation').validate({
            submitHandler: function (form) { //通过之后回调
                //进行ajax传值
                $.ajax({
                    data: $("#allInputsFormValidation").serialize(),
                    dataType: "text",
                    type: "post",
                    url: "/user/userupdate",
                    success: function (data) {
                        if (data == "1") {
                            swal("成功！", "修改成功，2s后退出登录。请重新登录", "success");
                            setTimeout(function () {
                                window.parent.location.href = "/user/loginout";
                            }, 2000)
                            //window.location.href = "/page/user_list";
                        } else {
                            swal("糟糕！", data, "error");
                        }
                    },
                    error: function () {
                        swal("出错了！", "服务器通信失败，请检查网络状态", "error");
                    }
                });
            },
        });


    });


</script>

<script>


</script>

</html>
